package shuashuami.hb.com.avtivity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.json.JSONException;
import org.json.JSONObject;

import shuashuami.hb.com.hbclient.R;
import shuashuami.hb.com.hdlibrary.util.ToastUtil;
import shuashuami.hb.com.http.HttpBMethods;
import shuashuami.hb.com.http.HttpOnNextListener;
import shuashuami.hb.com.http.LoginHttp;
import shuashuami.hb.com.share.ShareCUserInfo;

public class BShenqingTixianActivity extends AbActivity {

    private LinearLayout llToSaveZhifubao;
    private LinearLayout llToSaveYInhangka;

    private TextView tvZhifubao;
    private String zhifubaoZhanghao;

    private String yinhang;
    private TextView tvYInhang;

    private LinearLayout llSelectZhifubao, llSelectBank;
    private ImageView imgZHifubao, imgBank;

    private String type = "alipay";
    private EditText edtMoney;

    private Button btnTixian;

    @Override
    protected void initUI() {
        setAbContentView(R.layout.activity_cshenqing_tixian);
        llToSaveZhifubao = (LinearLayout) findViewById(R.id.ll_wodeyue_yongjinmingxi);
        tvZhifubao = (TextView) findViewById(R.id.tv_cshenqing_tixian_zhifubao_name);
        llToSaveYInhangka = (LinearLayout) findViewById(R.id.ll_wodeyue_tixianjilu);
        tvYInhang = (TextView) findViewById(R.id.tv_cshenqing_tixian_yinhangka_name);
        llSelectZhifubao = (LinearLayout) findViewById(R.id.ll_select_zhifubao);
        llSelectBank = (LinearLayout) findViewById(R.id.ll_select_bank);
        imgZHifubao = (ImageView) findViewById(R.id.img_zhifubao_xuanze);
        imgBank = (ImageView) findViewById(R.id.img_yinhangka_xuanze);
        edtMoney = (EditText) findViewById(R.id.edt_cshenqing_tixian_jine);
        btnTixian = (Button) findViewById(R.id.btn_cshenqing_tixian);


    }

    @Override
    protected void initData() {
        setTitleBar("申请提现");
        setLeftView();

    }

    @Override
    protected void onResume() {
        super.onResume();
        getZHifubaoMessage();
        getYinhangMessage();
    }

    @Override
    protected void initListener() {
        llToSaveZhifubao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(BShenqingTixianActivity.this, BAddZhifubaoActivity.class);
                startActivity(intent);
            }
        });
        llToSaveYInhangka.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(BShenqingTixianActivity.this, BAddYinHangKaActivity.class);
                startActivity(intent);
            }
        });
        llSelectZhifubao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imgZHifubao.setVisibility(View.VISIBLE);
                imgBank.setVisibility(View.INVISIBLE);
                type = "alipay";
            }
        });
        llSelectBank.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imgZHifubao.setVisibility(View.INVISIBLE);
                imgBank.setVisibility(View.VISIBLE);
                type = "bankcard";
            }
        });
        btnTixian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tixian();
            }
        });
    }

    //获取支付宝信息
    public void getZHifubaoMessage() {
        HttpOnNextListener<String> listener = new HttpOnNextListener<String>() {
            @Override
            public void onNext(String s) {
                Message message = handler.obtainMessage(1);     // Message
                message.obj = s;
                handler.sendMessageDelayed(message, 0);

            }
        };
        ShareCUserInfo shareCUserInfo = new ShareCUserInfo(this);
        HttpBMethods.getZhifubao(listener,
                shareCUserInfo.getId());
    }

    //获取银行卡信息
    public void getYinhangMessage() {
        HttpOnNextListener<String> listener = new HttpOnNextListener<String>() {
            @Override
            public void onNext(String s) {
                Message message = handler.obtainMessage(2);     // Message
                message.obj = s;
                handler.sendMessageDelayed(message, 0);

            }
        };
        ShareCUserInfo shareCUserInfo = new ShareCUserInfo(this);
        HttpBMethods.getBank(listener,
                shareCUserInfo.getId());
    }

    //提现
    public void tixian() {
        HttpOnNextListener<String> listener = new HttpOnNextListener<String>() {
            @Override
            public void onNext(String s) {
                Message message = handler.obtainMessage(3);     // Message
                message.obj = s;
                handler.sendMessageDelayed(message, 0);

            }
        };
        ShareCUserInfo shareCUserInfo = new ShareCUserInfo(this);
        HttpBMethods.tixian(listener,
                shareCUserInfo.getId(), edtMoney.getText().toString(), type);
    }

    final Handler handler = new Handler() {

        public void handleMessage(Message msg) {         // handle message
            switch (msg.what) {
                case 1:
                    String result = msg.obj.toString();
                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        if (jsonObject.getInt("status") == 203) {
                            LoginHttp loginHttp = new LoginHttp(BShenqingTixianActivity.this);
                            loginHttp.login();
                            return;
                        }
                        if (jsonObject.getInt("status") == 200) {
                            JSONObject content = jsonObject.getJSONObject("content");
                            if (content.getString("alipay_realname").equals("")) {
                                tvZhifubao.setText("添加支付宝");
                            } else {
                                tvZhifubao.setText(content.getString("alipay_realname"));
                            }
                            yinhang = content.getString("alipay_account");
                        } else {
                            ToastUtil.showShort(BShenqingTixianActivity.this, jsonObject.getString("message"));
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;

                case 2:
                    try {
                        JSONObject jsonObject = new JSONObject(msg.obj.toString());
                        if (jsonObject.getInt("status") == 203) {
                            LoginHttp loginHttp = new LoginHttp(BShenqingTixianActivity.this);
                            loginHttp.login();
                            return;
                        }
                        if (jsonObject.getInt("status") == 200) {
                            JSONObject content = jsonObject.getJSONObject("content");
                            if (content.getString("bank_id").equals("0")) {
                                tvYInhang.setText("添加银行卡");
                            } else {
                                tvYInhang.setText(content.getString("bank_name"));
                            }

                            yinhang = content.getString("bank_id");
                        } else {
                            ToastUtil.showShort(BShenqingTixianActivity.this, jsonObject.getString("message"));
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
                case 3:
                    try {
                        JSONObject jsonObject = new JSONObject(msg.obj.toString());
                        if (jsonObject.getInt("status") == 203) {
                            LoginHttp loginHttp = new LoginHttp(BShenqingTixianActivity.this);
                            loginHttp.login();
                            return;
                        }
                        if (jsonObject.getInt("status") == 200) {
                            ToastUtil.showShort(BShenqingTixianActivity.this, jsonObject.getString("message"));
                            setResult(RESULT_CANCELED);
                            finish();
                        } else {
                            ToastUtil.showShort(BShenqingTixianActivity.this, jsonObject.getString("message"));
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
            }

            super.handleMessage(msg);
        }
    };
}
